﻿using YunxSoft.AfterSchoolClazz.Converters;
using YunxSoft.AfterSchoolClazz.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace YunxSoft.AfterSchoolClazz.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public MainWindowViewModel(ExcelReader excelReader, IScheduleService scheduleService)
        {
            _excelReader = excelReader;
            _scheduleService = scheduleService;            
        }

        private readonly ExcelReader _excelReader;
        private readonly IScheduleService _scheduleService;

        public ObservableCollection<SelectableItem> ScheduleItems { get; private set; } = new ObservableCollection<SelectableItem>();
        public ObservableCollection<SelectableItem> StudentScheduleItems { get; private set; } = new ObservableCollection<SelectableItem>();

        public string About => "公司：湛江市云行科技有限公司\r\n邮箱：987265644@qq.com\r\n微信：ppz_zpp\r\n电话：15889838173";
        public string StatusBarDispaly => "湛江市云行科技有限公司";
        public string Title => $"签到表生成器({Assembly.GetExecutingAssembly().GetName().Version.ToString()})";

        private string _scheduleFilePath;
        public string ScheduleFilePath
        {
            get => _scheduleFilePath;
            set => SetProperty(ref _scheduleFilePath, value);
        }

        private string _studentScheduleFilePath;
        public string StudentScheduleFilePath
        {
            get => _studentScheduleFilePath;
            set => SetProperty(ref _studentScheduleFilePath, value);
        }

        private string _notifyMessage;
        public string NotifyMessage
        {
            get => _notifyMessage;
            set => SetProperty(ref _notifyMessage, value);
        }

        private ExportType _selectedExportType;
        public ExportType SelectedExportType
        {
            get => _selectedExportType;
            set => SetProperty(ref _selectedExportType, value);
        }

        public bool CheckExportSignInSheet()
        {
            NotifyMessage = "";
            if (string.IsNullOrEmpty(ScheduleFilePath))
            {
                NotifyMessage = "请上传课程数据";
                return false;
            }
            if (string.IsNullOrEmpty(StudentScheduleFilePath))
            {
                NotifyMessage = "请上传报名数据";
                return false;
            }

            var schedules = _scheduleService.GetSchedules(ScheduleFilePath, ScheduleItems.First(x => x.IsSelected).DisplayText);
            var studentSchedules = _scheduleService.GetStudentSchedules(StudentScheduleFilePath, StudentScheduleItems.Where(x => x.IsSelected).Select(x => x.DisplayText).ToArray());

            var validateResult = _scheduleService.ValidateCourse(schedules, studentSchedules);
            if (!string.IsNullOrEmpty(validateResult))
            {
                NotifyMessage = validateResult;
                return false;
            }

            return true;
        }

        public void ExportSignInSheet(string saveFilePath)
        {
            var schedules = _scheduleService.GetSchedules(ScheduleFilePath, ScheduleItems.First(x => x.IsSelected).DisplayText);
            var studentSchedules = _scheduleService.GetStudentSchedules(StudentScheduleFilePath, StudentScheduleItems.Where(x => x.IsSelected).Select(x => x.DisplayText).ToArray());

            _scheduleService.GenerateAttendanceSheet(schedules, studentSchedules, SelectedExportType, saveFilePath);
        }

        public void OpenFileSchedule(string filePath)
        {
            var sheetNames = _excelReader.GetSheetNames(filePath);
            ScheduleItems.Clear();
            foreach (var sheetName in sheetNames)
            {
                ScheduleItems.Add(new SelectableItem
                { 
                    DisplayText = sheetName,
                    IsSelected = sheetName == "总课表"
                });
            }
            ScheduleFilePath = filePath;
        }

        public void OpenFileStudentSchedule(string filePath)
        {
            var sheetNames = _excelReader.GetSheetNames(filePath);
            StudentScheduleItems.Clear();
            foreach (var sheetName in sheetNames)
            {
                StudentScheduleItems.Add(new SelectableItem 
                { 
                    DisplayText = sheetName,
                    IsSelected = sheetName.Contains("年级")
                });
            }
            StudentScheduleFilePath = filePath;
        }
    }
}
